﻿using MediatR;
using Traceability.App.Api.Application.Command.Detail.Slaughter;
using Traceability.App.ErrorCount;
using Traceability.App.InfrasIntructure;
using Traceability.App.Model.Slaughter;

namespace Traceability.App.Api.Application.Handler.Detail.Slaughter
{
    public class GetSlaughterCommpanyHandler : IRequestHandler<GetSlaughterCommpanyCommand, ApiResult<SlaughterCommpanyModel>>
    {
        IBaseRepository<SlaughterCommpanyModel> slaucomRepo;

        public GetSlaughterCommpanyHandler(IBaseRepository<SlaughterCommpanyModel> slaucomRepo)
        {
            this.slaucomRepo = slaucomRepo;
        }

        public Task<ApiResult<SlaughterCommpanyModel>> Handle(GetSlaughterCommpanyCommand request, CancellationToken cancellationToken)
        {
            ApiResult<SlaughterCommpanyModel> res = new ApiResult<SlaughterCommpanyModel>();
            var ssss = slaucomRepo.GetAll().Where(c => c.Batch == request.Batch).OrderByDescending(c => c.Id).FirstOrDefault();
            if (ssss == null)
            {
                res.Msg = "错误";
                res.Code = ApiEnum.Fail;
                return Task.FromResult(res);
            }
            res.Msg = "成功";
            res.Code = ApiEnum.Success;
            res.Datas = ssss;
            return Task.FromResult(res);
        }
    }
}
